<?php 


class Upload extends Base{
	
	function __construct($file,$option=array()){
		
		
		
		$this->options = $this->init_config($option);
		$this->dir = 'res/';
		$this->err_arr = array();
		
		if(count($file)<=0){
			$this->addError('请选择文件');
			return false;
		}
		$this->FILE = $file[key($file)];
		
	}
	function get_image_info($path){
		$info = getimagesize($path);
		$mime = isset($info['mime'])?$info['mime']:'';
		if(!$mime){
			return false;
		}
		
	}
	function init_config($opt){
		$arr = array(
			'def_folder'	=>'',
			'max_size'	=>10,
			'upload'	=>'upload',
			'is_sub'	=>1,
			'rename'	=>'',
			
		);
		
		return $arr;
	}
	
	
	
	function run(){
		
		if(!isset($this->FILE['name'])){
			$this->addError('必须选择文件');
			return false;
		}
		
		
		if(is_array($this->FILE['name'])){
			$is_ok = true;
			
			foreach($this->FILE['name'] as $key=>$val){
				if(!$this->check_filesize($this->FILE['size'][$key])){
					$this->addError('必须选择文件');
					$is_ok = false;
				}
				
				$ls_file = $this->get_filename($this->FILE['name'][$key]);
				
				$path = $this->get_dir($ls_file['ext']);
				
				if(!empty($this->options['rename'])){
					//如果自定义名称
					$ls_name = $this->options['rename'];
					$file = $path.$ls_name.'.'.$ls_file['ext'];
				}else{
					
					while(true){
						$ls_name = time().rand(10000,99999);
						$file = $path.$ls_name.'.'.$ls_file['ext'];
						if(!file_exists($file)){
							break;
						}
					}
				}
				
				$this->FILE['target'][$key] = $file;
				
			}
			
			if(!$is_ok){
				return false;
			}
			
			$arr = array();
			foreach($this->FILE['name'] as $key=>$val){
				$file = $this->FILE['target'][$key];
				if (!move_uploaded_file($this->FILE["tmp_name"][$key],$file)){
					$this->addError('文件复制错误，请查看权限是否错误');
					return false;
				}
				$ls_file = $this->get_filename($this->FILE['name'][$key]);
				
				$arr[] = array('path'=>$file,
							 'name'=>$this->FILE['name'][$key],
							 'size'=>$this->FILE['size'][$key],
							 'ext'=>$ls_file['ext'],
							 'type'=>$this->get_type($ls_file['ext'])
					);
				

			}
			
			return $arr;
		}
		else{
			if(!$this->check_filesize($this->FILE['size'])){
				$this->addError('必须选择文件');
				$is_ok = false;
			}
			
			$ls_file = $this->get_filename($this->FILE['name']);
			
			$path = $this->get_dir($ls_file['ext']);
			
			if(!empty($this->options['rename'])){
				//如果自定义名称
				$ls_name = $this->options['rename'];
				$file = $path.$ls_name.'.'.$ls_file['ext'];
				
			}else{
				
				while(true){
					$ls_name = time().rand(10000,99999);
					$file = $path.$ls_name.'.'.$ls_file['ext'];
					if(!file_exists($file)){
						break;
					}
				}
			}
			if (!move_uploaded_file($this->FILE["tmp_name"],$file)){
				$this->addError('文件复制错误，请查看权限是否错误');
				return false;
			}

			
			$arr[] = array('path'=>$file,
						 'name'=> $this->FILE['name'],
						 'size'=>$this->FILE['size'],
						 'ext'=>$ls_file['ext'],
						 'type'=>$this->get_type($ls_file['ext'])
				);
			
			return $arr;
		}
		
		
		
		return false;
	}
	function get_dir($ext){
		
		$dir = "upload";
		
		if(!empty($this->options['def_folder'])){
			$dir = $this->options['def_folder'];
		}
		$type = $this->get_type($ext);
		
		
		$path   = $this->dir .$dir."/".$type.'/';

		if(!is_dir($path)){
			mkdir($path,0777,true);
		}

		if($this->options['is_sub']){
			$date = date('Ymd');
			$path .= $date."/";
			
			if(!is_dir($path)){
				mkdir($path,0777,true);
			}
		}
		
		return $path;
	}
	function get_filename($filename){
		
		$fg = explode('.',$filename);
		$ext  = end($fg);
		$name = substr($filename,0,strlen($filename)-strlen($ext));
		
		return array('name'=>$name,'ext'=>strtolower($ext));
	}
	function check_filesize($size){
		$max_size = $this->options['max_size'] * 1024 *1024;
		if($size>$max_size){
			return false;
		}
		return true;
	}
	function addError($msg){
		$this->err_arr[] = $msg;
	}
	function getError(){
		return implode(PHP_EOL ,$this->err_arr);
	}
}

?>